(() => {
    let ele = {};

    ele.list = $('#list')
    ele.searchBtn = $('#searchBtn')
    ele.searchID = $('#searchID')
    ele.list_empty = $('#list-empty')
    // 玩家信息弹框
    ele.modal_message = $('#modal-message')
    ele.close_message = $('#close-message')
    ele.header = $('#header')//弹框头像
    ele.nickname = $('#nickname')//弹框昵称
    ele.vip = $('#vip')//弹框VIP显示
    ele.familyLevel = $('#familyLevel')//弹框家族等级
    ele.myinfo = $('#myinfo')//弹框区服
    ele.ID = $('#ID')//弹框ID
    ele.IDS = $('.IDS')//弹框复制信息
    ele.weixin = $('#weixin')//弹框微信
    ele.QQ = $('#QQ')//弹框QQ
    ele.copy = $('.copy')////弹框复制

    ele.close_message.onclick = () => {
        ele.modal_message.style.display = 'none'
    }
    // 搜索
    ele.searchBtn.onclick = () => {
        let searchID = ele.searchID.value//ID
        console.log(searchID.length);
        if (!/^\d{1,}$/.test(searchID)) {
            openTips('ID不正确')
            return false;
        }
        if (searchID.length != 6) {
            openTips('ID不正确')
            return false;
        }
        getFriendList(searchID)
    }



    // 拉取列表
    function getFriendList(ID) {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.ZHITUISEARCH,
            method: 'POST',
            header: {
                Authorization: token
            },
            data: {
                invite_code: ID
            },
            success: res => {
                if (res.code == 1) {
                    drawFriendList(res.data)
                } else openTips(res.msg);
            }
        });
    }

    // 渲染好友列表
    function drawFriendList(data) {
        if (data.length == 0) {
            openTips('您的直推好友中不存在此用户')
            ele.list_empty.style.display = 'block'
        } else {
            ele.list_empty.style.display = 'none'
        }
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let d = data[i];
            html += '<div class="jiav">' +
                '<div class="jsaj">' +
                '<img class="toux" src="' + d.head_pic + '" alt="">' +
                '<p class="issh">' + d.label + '</p>' +
                '<div class="jial">' +
                '<p class="j1">' + d.nick_name + '</p>' +
                '<p class="j2">' + getTime(d.register_at, 'YYYY-MM-DD HH:mm:ss') + '</p>' +
                '</div>' +
                '</div>' +
                '<div class="sale" onclick=submits(' + JSON.stringify(d) + ')>' +
                '<div class="salen">查看' + '</div>' +
                '</div>' +
                '</div>'
        }
        ele.list.innerHTML = html;

    }

    // 查看弹框信息
    window.submits = submits
    function submits(data) {
        // console.log(data);
        ele.header.src = data.head_pic//弹框头像
        ele.nickname.innerHTML = data.nick_name//弹框昵称
        if (data.is_vip == 1) {
            ele.vip.style.display = 'block'
        } else {
            ele.vip.style.display = 'none'
        }
        ele.familyLevel.innerHTML = data.family_level + '级家族'//弹框家族等级
        ele.myinfo.innerHTML = (data.server_name || "暂无") + ' | ' + (data.role_name || "暂无") //弹框区服
        ele.ID.innerHTML = data.invite_code || '暂未设置'//弹框ID
        ele.weixin.innerHTML = data.wechat || '暂未设置'//弹框微信
        ele.QQ.innerHTML = data.qq || '暂未设置'//弹框QQ
        ele.modal_message.style.display = 'block'
    }
    // 复制
    for (let i = 0; i < ele.copy.length; i++) {
        ele.copy[i].onclick = () => {
            let flag = copy(ele.IDS[i].innerText);
            if (flag) openTips("复制成功");
            else openTips("复制失败，请手动复制");
        }
    }

    function getTime(timestamp, format) {
        if (String(timestamp).length === 10) {
            timestamp = timestamp * 1000
        }
        var date = new Date(timestamp)
        var Y = date.getFullYear()
        var M = date.getMonth() + 1
        var D = date.getDate()
        var hour = date.getHours()
        var min = date.getMinutes()
        var sec = date.getSeconds()
        if (format === 'YYYY') {
            return Y // 2021
        } else if (format === 'YYYY-MM') { // 2021-07
            return Y + '-' + (M < 10 ? '0' + M : M)
        } else if (format === 'YYYY-MM-DD') { // 2021-07-12
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D)
        } else if (format === 'HH:mm:ss') { // 10:20:35
            return (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else if (format === 'YYYY-MM-DD HH:mm') { // 2021-07-12 10:20
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min)
        } else if (format === 'YYYY-MM-DD HH:mm:ss') { // 2021-07-12 10:20:35
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else {
            return '--'
        }

    }

})();